<template>
	<view class="wrap">
		<u-form :model="model" :rules="rules" ref="uForm" :errorType="errorType">
			<u-form-item :leftIconStyle="{color: '#888', fontSize: '32rpx'}" label-width="120" :label-position="labelPosition" label="昵称" prop="name">
				<u-input :border="border" placeholder="请输入昵称" v-model="model.name" type="text"></u-input>
			</u-form-item>
			<u-form-item :label-position="labelPosition" label="性别" prop="sex">
				<u-input :border="border" type="select" :select-open="actionSheetShow" v-model="model.sex" placeholder="请选择性别" @click="actionSheetShow = true"></u-input>
			</u-form-item>
			
			<u-form-item :rightIconStyle="{color: '#888', fontSize: '32rpx'}" right-icon="kefu-ermai" :label-position="labelPosition" label="手机号码" label-width="150">
				<u-input :border="border" placeholder="请输入手机号" v-model="model.phone"></u-input>
			</u-form-item>
			<u-form-item :rightIconStyle="{color: '#888', fontSize: '32rpx'}"  :label-position="labelPosition" label="身份证号" label-width="150">
				<u-input :border="border" placeholder="请输入身份证" v-model="model.card"></u-input>
			</u-form-item>
			
			<u-form-item :label-position="labelPosition" label="上传头像" prop="photo" label-width="150">
				<u-avatar :src="base+avater"></u-avatar>
				<u-upload width="160" height="160" :auto-upload="false" @on-choose-complete="selsect"></u-upload>
			</u-form-item>
		</u-form>
		
		<u-button @click="submit">提交</u-button>
		<u-action-sheet :list="actionSheetList" v-model="actionSheetShow" @click="actionSheetCallback"></u-action-sheet>
		<u-picker mode="region" v-model="pickerShow" @confirm="regionConfirm"></u-picker>
		<u-verification-code seconds="60" ref="uCode" @change="codeChange"></u-verification-code>
		
	</view>
</template>

<script>
	import api from 'commer/apiURL.js'
export default {
	data() {
		let that = this;
		return {
			image:'',
			avater:'',
			base:api.baseurl,
			model: {
				name: '',
				sex: '',
				likeFruit: '',
				intro: '',
				payType: '支付宝',
				agreement: false,
				region: '',
				goodsType: '',
				phone: '',
				phone2:'',
				phone3:'',
				code: '',
				password: '',
				rePassword: '',
				remember: false,
				photo: '',
				card:'',
				card2:'',
				card3:''
			},
			rules: {
				name: [
					
					{
						// 此为同步验证，可以直接返回true或者false，如果是异步验证，稍微不同，见下方说明
						validator: (rule, value, callback) => {
							// 调用uView自带的js验证规则，详见：https://www.uviewui.com/js/test.html
							return this.$u.test.chinese(value);
						},
						message: '姓名必须为中文',
						// 触发器可以同时用blur和change，二者之间用英文逗号隔开
						trigger: ['change','blur'],
					}
				],
				sex: [
					{
						required: true,
						message: '请选择性别',
						trigger: 'change'
					},
				],
			},
			border: false,
			check: false,
			selectStatus: 'close',
			actionSheetList: [
				{
					text: '男'
				},
				{
					text: '女'
				},
				{
					text: '保密'
				}
			],
			actionSheetShow: false,
			pickerShow: false,
			selectShow: false,
			radioCheckWidth: 'auto',
			radioCheckWrap: false,
			labelPosition: 'left',
			codeTips: '',
			errorType: ['message'],
		};
	},
	onLoad() {
		this.getinfo()
	},
	computed: {
		borderCurrent() {
			return this.border ? 0 : 1;
		}
	},
	onReady() {
		this.$refs.uForm.setRules(this.rules);
	},
	methods: {
		purrr(){
			
		},
		selsect(e){
			let img = e[0].file.path
			this.shang(img)
			
		},
		shang(e){
			let token = uni.getStorageSync('token')
			uni.uploadFile({
				url:this.base+'/prod-api/common/upload',
				name:'file',
				filePath:e,
				header:{
					'authorization':token
				},
				success:(res)=>{
					let date = JSON.parse(res.data)
					if(date.code == 200){
						this.image = '/prod-api'+date.fileName
						
					}else{
						uni.showToast({
							title:'修改失败',
							icon:'none'
						})
					}
				},fail:(err)=>{
					console.log('shibai ')
				}
			})
		},
		//手机号星号显示
		finlxing(str){
			let enstr = str.substring(7,0)+'****'
			return enstr
		},
		finlxing2(str){
			let enstr = str.substring(6,0)+'************'
			return enstr
		},
		getinfo(){
			this.$get(this.base+'/prod-api/api/common/user/getInfo').then((res)=>{
				this.info = res.user
				this.model.name = res.user.nickName
				this.model.phone2 = res.user.phonenumber
				this.model.phone = this.finlxing(res.user.phonenumber)
				this.model.phone3 = this.finlxing(res.user.phonenumber)
				this.model.sex = res.user.sex==1?'女':'男'
				this.model.card = this.finlxing2(res.user.idCard)
				this.model.card3 = this.finlxing2(res.user.idCard)
				this.model.card2 = res.user.idCard
				this.avater = res.user.avatar
			})
		},
		submit() {
			if(this.model.phone == this.model.phone3 && this.model.card == this.model.card3){
				console.log('不变')
			}else{
				if(this.model.card.indexOf('*')>0){
					uni.showToast({
						title:'请输入正确的身份证',
						icon:"none"
					})
					return
				}else if(this.model.phone.indexOf('*')>0){
					uni.showToast({
						title:'请输入正确的手机号',
						icon:"none"
					})
					return
				}else{
					this.model.card2 = this.model.card
					this.model.phone2 = this.model.phone
				}
			}
			this.$refs.uForm.validate(valid => {
				if (valid) {
					this.$put(this.base+'/prod-api/api/common/user',{
						"nickName": this.model.name,
						"phonenumber": this.model.phone2,
						"idCard": this.model.card2,
						"sex": this.model.sex =='女'?1:0,
						"avatar":this.image
					}).then((res)=>{		
						if(res.code == 200){
							uni.showToast({
								title:"修改成功",
							})
							this.getinfo()
							setTimeout(()=>{
								uni.switchTab({
									url:'../mine'
								})
							},500)
						}else{
							uni.showToast({
								title:res.msg,
								icon:'none'
							})
						}
					})
					console.log('验证通过');
				} else {
					console.log('验证失败');
				}
			});
		},
		// 点击actionSheet回调
		actionSheetCallback(index) {
			uni.hideKeyboard();
			this.model.sex = this.actionSheetList[index].text;
		},
		// checkbox选择发生变化
		checkboxGroupChange(e) {
			this.model.likeFruit = e;
		},
		// radio选择发生变化
		radioGroupChange(e) {
			this.model.payType = e;
		},
		// 勾选版权协议
		checkboxChange(e) {
			this.model.agreement = e.value;
		},
		// 选择地区回调
		regionConfirm(e) {
			this.model.region = e.province.label + '-' + e.city.label + '-' + e.area.label;
		},
		// 选择商品类型回调
		selectConfirm(e) {
			this.model.goodsType = '';
			e.map((val, index) => {
				this.model.goodsType += this.model.goodsType == '' ? val.label : '-' + val.label;
			})
		},
		borderChange(index) {
			this.border = !index;
		},
		radioCheckboxChange(index) {
			if(index == 0) {
				this.radioCheckWrap = false;
				this.radioCheckWidth = 'auto';
			} else if(index == 1) {
				this.radioCheckWrap = true;
				this.radioCheckWidth = 'auto';
			} else if(index == 2) {
				this.radioCheckWrap = false;
				this.radioCheckWidth = '50%';
			}
		},
		labelPositionChange(index) {
			this.labelPosition = index == 0 ? 'left' : 'top';
		},
		codeChange(text) {
			this.codeTips = text;
		},
		// 获取验证码
		getCode() {
			if(this.$refs.uCode.canGetCode) {
				// 模拟向后端请求验证码
				uni.showLoading({
					title: '正在获取验证码',
					mask: true
				})
				setTimeout(() => {
					uni.hideLoading();
					// 这里此提示会被this.start()方法中的提示覆盖
					this.$u.toast('验证码已发送');
					// 通知验证码组件内部开始倒计时
					this.$refs.uCode.start();
				}, 2000);
			} else {
				this.$u.toast('倒计时结束后再发送');
			}
		},
		errorChange(index) {
			if(index == 0) this.errorType = ['message'];
			if(index == 1) this.errorType = ['toast'];
			if(index == 2) this.errorType = ['border-bottom'];
			if(index == 3) this.errorType = ['border'];
		}
	}
};
</script>

<style scoped lang="scss">
.wrap {
	padding: 30rpx;
}

.agreement {
	display: flex;
	align-items: center;
	margin: 40rpx 0;

	.agreement-text {
		padding-left: 8rpx;
		color: $u-tips-color;
	}
}
</style>
